/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 */

package pl.droidsonroids.gif.utils;

import ohos.agp.utils.Rect;
import ohos.agp.utils.RectFloat;

/**
 * 功能描述
 *

 * @since 2020-06-29
 */
public class RectFloatHarmony extends RectFloat implements RectFloatInterface {
    /**
     * left
     */
    public float left;
    /**
     * top
     */
    public float top;
    /**
     * right
     */
    public float right;
    /**
     * bottom
     */
    public float bottom;

    /**
     * RectFloatHarmony
     */
    public RectFloatHarmony() {
        super();
    }

    public RectFloatHarmony(float left, float top, float right, float bottom) {
        super(left, top, right, bottom);
        this.left = left;
        this.bottom = bottom;
        this.right = right;
        this.top = top;
    }

    @Override
    public boolean intersect(float left, float top, float right, float bottom) {
        if (this.left < right && left < this.right && this.top < bottom && top < this.bottom) {
            if (this.left < left) {
                this.left = left;
            }
            if (this.top < top) {
                this.top = top;
            }
            if (this.right > right) {
                this.right = right;
            }
            if (this.bottom > bottom) {
                this.bottom = bottom;
            }
            return true;
        }
        return false;
    }

    @Override
    public boolean intersect(RectFloatHarmony r) {
        return intersect(r.left, r.top, r.right, r.bottom);
    }

    /**
     * @return the rectangle's width. This does not check for a valid rectangle
     * (i.e. left <= right) so the result may be negative.
     */
    public final float width() {
        return right - left;
    }

    /**
     * @return the rectangle's height. This does not check for a valid rectangle
     * (i.e. top <= bottom) so the result may be negative.
     */
    public final float height() {
        return bottom - top;
    }

    @Override
    public void union(float left, float top, float right, float bottom) {
        if ((left < right) && (top < bottom)) {
            if ((this.left < this.right) && (this.top < this.bottom)) {
                if (this.left > left) {
                    this.left = left;
                }
                if (this.top > top) {
                    this.top = top;
                }
                if (this.right < right) {
                    this.right = right;
                }
                if (this.bottom < bottom) {
                    this.bottom = bottom;
                }
            } else {
                this.left = left;
                this.top = top;
                this.right = right;
                this.bottom = bottom;
            }
        }
    }

    @Override
    public void union(RectFloatHarmony r) {
        union(r.left, r.top, r.right, r.bottom);
    }

    @Override
    public void set(float left, float top, float right, float bottom) {
        this.left = left;
        this.top = top;
        this.right = right;
        this.bottom = bottom;
    }

    public void set(RectFloatHarmony r)
    {
        this.left   = r.left;
        this.top    = r.top;
        this.right  = r.right;
        this.bottom = r.bottom;
    }

    /**
     * Copy the coordinates from src into this rectangle.
     *
     * @param src The rectangle whose coordinates are copied into this
     *           rectangle.
     */
    public void set(Rect src) {
        this.left   = src.left;
        this.top    = src.top;
        this.right  = src.right;
        this.bottom = src.bottom;
    }

    /**
     * Returns true if the rectangle is empty (left >= right or top >= bottom)
     */
    // TODO using parent class isEmpty(), verify
    /*public final boolean isEmpty() {
        return left >= right || top >= bottom;
    }*/
}
